package com.itheima.hello.hellopromax高级进阶.斗地主;


/*
//斗地主
  54张牌
* 点数:"3","4","5","6","7","8","9","10","J","Q","K","A","2"
* 花色:"♦","♥","♠","♣"
* 大小王:"♞","♘"
* 底牌3张
* */

import java.util.*;

public class room {
    //每次创建一个房间都会生成一副牌
    private List<card> newCard = new ArrayList<>();

    public room() {
        //在无参构造器中生成衣服新牌
        //点数  个数  类型确定
        String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] color = {"♦","♥","♠","♣"};
        int size = 0;//大小
        //先遍历点数再遍历花色
        for (String num : number) {
            size++;
            for (String c : color) {
                card card = new card(num, c, size);
                newCard.add(card);//52张牌
            }}
        card smallKing = new card("","♘",++size);//创建小王
        card bigKing = new card("","♞",++size);//创建大王
//                newCard.add(smallKing);//加入小王
//                newCard.add(bigKing);//加入大王
        Collections.addAll(newCard,smallKing,bigKing);//往新牌数组里加入小大王
        System.out.println("新牌:" + newCard);
    }

    //游戏启动方法
    public void start() {
        Collections.shuffle(newCard);
        System.out.println("洗牌后:" + newCard);
        //定义三个玩家,定义三个Arraylist集合
        List<card> zhangsan = new ArrayList<>();
        List<card> lisi = new ArrayList<>();
        List<card> wangwu = new ArrayList<>();
        //发牌
        System.out.println("-----------发牌-----------------");
        for (int i = 0; i < newCard.size()-3; i++) {//发出51张牌，留三张底牌
            card card = newCard.get(i);
            if (i%3==0){
                zhangsan.add(card);
            } else if (i%3==1) {
                lisi.add(card);
            }else if (i%3==2){
                wangwu.add(card);
            }
        }
        //排序
        System.out.println("-----------看牌------------------");
        sortCard(zhangsan);
        sortCard(lisi);
        sortCard(wangwu);
        //看牌
        System.out.println("张三:" + zhangsan);
        System.out.println("李四:" + lisi);
        System.out.println("王五:" + wangwu);
        List<card> lastCards = newCard.subList(newCard.size() - 3, newCard.size());//底牌
        System.out.println("底牌:" + lastCards);
        System.out.println("---------抢地主------------------");
        zhangsan.addAll(lastCards);//张三抢地主
        sortCard(zhangsan);
        System.out.println("张三抢到地主后:"+zhangsan);//抢到地主后对牌进行排序
    }
    //排序方法
    private void sortCard(List<card> array) {
//        Collections.sort(array, new Comparator<card>() {         //匿名内部类写法
//            @Override
//            public int compare(card o1, card o2) {
//                return o2.getSize()- o1.getSize();//降序
////                return o1.getSize()- o2.getSize();//升序
//            }
//        });
        Collections.sort(array, (o1, o2) -> (o2.getSize() - o1.getSize()));//lambda简化写法
}}
